package com.hs.color.lamp;


import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.chiemy.cardview.R;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class MusicFragment extends Fragment {
	
	public static  MediaPlayer mediaPlayer;
	private List<String> audioList = new ArrayList<String>();
	private LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
	private int currentItem = 0;
	private ImageView pause;
	
	
	private View root;
	private Context context;
	

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.context = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		root = View.inflate(getActivity(), R.layout.music_frag, null);
		mediaPlayer = new MediaPlayer();
		ImageView next = (ImageView) root.findViewById(R.id.next);
        pause = (ImageView) root.findViewById(R.id.pause);
        pause.setEnabled(false);
        ImageView pre = (ImageView) root.findViewById(R.id.pre);
        //Button stop = (Button) root.findViewById(R.id.stop);
        audioList();
        
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				nextMusic();
			}
		});
//        stop.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(mediaPlayer.isPlaying()){
//					mediaPlayer.stop();
//				}
//				pause.setText("播放");
//			}
//		});
        
        
        
        pause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mediaPlayer.isPlaying()){
					mediaPlayer.pause();
					//((Button)v).setText("播放");
					pause.setImageResource(R.drawable.btn_stop_nor);
				}else{
					mediaPlayer.start();
					//((Button)v).setText("暂停");
					pause.setImageResource(R.drawable.btn_play_nor);
					
				}

			}
		});
        next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				nextMusic();
			}
		});
        pre.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				preMusic();
			}
		});
        /*************************************************************/
        
//        final AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        ((Activity) context).setVolumeControlStream(AudioManager.STREAM_MUSIC);
        //SeekBar seekBar = (SeekBar) root.findViewById(R.id.seekBar);
        //seekBar.setMax(am.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
//        int progress = am.getStreamVolume(AudioManager.STREAM_MUSIC);
//        seekBar.setProgress(progress);
//        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
//			
//			@Override
//			public void onStopTrackingTouch(SeekBar seekBar) {
//				
//			}
//			
//			@Override
//			public void onStartTrackingTouch(SeekBar seekBar) {
//				
//			}
//			
//			@Override
//			public void onProgressChanged(SeekBar seekBar, int progress,
//					boolean fromUser) {
//				am.setStreamVolume(AudioManager.STREAM_MUSIC, progress, AudioManager.FLAG_PLAY_SOUND);
//			}
//		});
        /*************************************************************/
		return root;
	}
	
	
	protected void nextMusic() {
		if(++currentItem >= audioList.size()){
			currentItem = 0;
		}

		String path = map.get(audioList.get(currentItem));
		playMusic(path);
	}
	protected void preMusic() {
		if(--currentItem >= 0){
			if(currentItem >= audioList.size()){
				currentItem = 0;
			}
		}else{
			currentItem = audioList.size() - 1;
		}
		String path = map.get(audioList.get(currentItem));
		playMusic(path);
	}
	private void audioList() {
		getFiles("/sdcard/Music/");
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_list_item_1,audioList);
		ListView listView = (ListView) root.findViewById(R.id.listView1);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> viewList, View view, 
					int position,long id) {
				currentItem = position;
				String path = map.get(audioList.get(position));
				playMusic(path);
			}
		});
	}
	protected void playMusic(String path) {
		
		try {
			if(mediaPlayer.isPlaying()){
				mediaPlayer.stop();
			}
			mediaPlayer.reset();//重置
			mediaPlayer.setDataSource(path);//指定要播放的音频文件
			mediaPlayer.prepare();//预加载音频文件
			mediaPlayer.start();
			//pause.setText("暂停");
			pause.setImageResource(R.drawable.btn_play_nor);
			pause.setEnabled(true);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void getFiles(String path) {
		File files = new File(path);
		File[] file = files.listFiles();
		try {
			for (File f : file) {
				if(f.isDirectory()){
					getFiles(f.getAbsolutePath());
				}else{
					if(f.length()>512)
					if(isAudioFile(f.getPath())){
						audioList.add(f.getPath().substring(f.getPath().lastIndexOf("/")+1));
						map.put(f.getPath().substring(f.getPath().lastIndexOf("/")+1), f.getPath());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static String[] imageFormatSet = new String[]{"ape", "mp3", "wav" };
	private boolean isAudioFile(String path) {
		for(String format:imageFormatSet){
			if(path.contains(format)){
				return true;
			}
		}
		return false;
	}
	
	
	
	
}
